import { getDetailByKey, getPageList } from '@/api/iot/product';
import { validatenull } from '@/utils/validate';

export const option = (safe) => {
  const validateName = (rule, value, callback) => {
    if (validatenull(value)) {
      callback();
    }
    const regex = /^[a-zA-Z0-9_\-:.@]{4,32}$/;
    const isValid = regex.test(value);
    if (!isValid) {
      callback(new Error('请确保您输入英文字母，并可包含数字、下划线（_）、中划线（-）、点号（.）、半角冒号（:）以及特殊字符@，整体长度需控制在4至32个字符之间'));
      return;
    }
    callback();
  };
  const validateNoteName = (rule, value, callback) => {
    if (validatenull(value)) {
      callback();
    }
    const regex = /^[a-zA-Z\u4e00-\u9fa5\d_]{4,64}$/;
    const isValid = regex.test(value);
    if (!isValid) {
      callback(new Error('请确保您输入中文或英文字母，并可包含数字和下划线（_），长度限制为4至64个字符'));
      return;
    }
    callback();
  };
  const validateUniqueNo = (rule, value, callback) => {
    const regex = /^[a-zA-Z\d]{1,32}$/;
    const isValid = regex.test(value);
    if (!isValid) {
      callback(new Error('请确保您输入的是英文字母或数字，并且长度不超过32个字符'));
      return;
    }
    callback();
  };
  return {
    height: 'auto',
    calcHeight: 32,
    grid: true,
    gridSpan: 8,
    tip: false,
    searchShow: true,
    searchMenuSpan: 6,
    searchIndex: 3,
    searchIcon: true,
    border: true,
    index: true,
    viewBtn: false,
    editBtn: false,
    delBtn: false,
    selection: true,
    labelWidth: 125,
    menuWidth: 300,
    searchLabelWidth: 125,
    dialogType: 'drawer',
    dialogWidth: 850,
    dialogClickModal: false,
    gridBackground: (row, index) => {
      if (row.deviceStatus === 1 && row.status === 1) {
        return 'linear-gradient(to bottom, rgba(88, 158, 258, 0.1), white)';
      } else if (row.deviceStatus === 2 && row.status === 1) {
        return 'linear-gradient(to bottom, rgba(228, 88, 88, 0.1), white)';
      } else {
        return 'linear-gradient(to bottom, rgba(128, 128, 128, 0.1), white)';
      }
    },
    column: [
      {
        label: 'DeviceName',
        labelTip: '英文字母，并可包含数字、下划线（_）、中划线（-）、点号（.）、半角冒号（:）以及特殊字符@，整体长度需控制在4至32个字符之间',
        prop: 'deviceName',
        type: 'input',
        span: 24,
        gridRow: true,
        slot: true,
        search: true,
        rules: [{
          required: false,
          trigger: 'change',
          validator: validateName
        }]
      },
      {
        label: '备注名称',
        labelTip: '中文或英文字母，并可包含数字和下划线（_），长度限制为 4 ~ 64 个字符',
        prop: 'deviceNoteName',
        type: 'input',
        span: 24,
        gridRow: true,
        rules: [{
          required: false,
          trigger: 'change',
          validator: validateNoteName
        }]
      },
      {
        label: '唯一编号',
        labelTip: '设备唯一信息编号，比如mac地址，sn码等，标识设备唯一性',
        prop: 'uniqueNo',
        type: 'input',
        hide: true,
        slot: true,
        span: 24,
        search: true,
        gridRow: true,
        rules: [{
          required: true,
          trigger: 'change',
          validator: validateUniqueNo
        }]
      },
      {
        label: '所属产品',
        labelTip: '可选择已发布的产品数据，将设备绑定后继承其物模型',
        prop: 'productKey',
        type: 'table',
        search: true,
        editDisabled: true,
        children: {
          border: true,
          column: [
            {
              label: '产品id',
              prop: 'id',
              hide: true
            },
            {
              label: '产品名称',
              prop: 'productName',
              search: true
            },
            {
              label: '产品编码',
              prop: 'productKey',
              search: true
            },
            {
              label: '产品密钥',
              prop: 'productSecret',
              search: true
            },
            {
              label: '上线时间',
              prop: 'updateTime'
            }
          ]
        },
        formatter(row) {
          if (!row.id) return '';
          return row.productName + ':' + row.productKey;
        },
        onLoad: ({ page, value, data }, callback) => {
          let param = {};
          let currentPage = 1;
          let pageSize = 10;

          if (value) {
            getDetailByKey(value).then(res => {
              callback(res.data.data);
            });
            return;
          }
          if (data) {
            param = data;
          }
          if (page) {
            currentPage = page.currentPage;
            pageSize = page.pageSize;
          }
          getPageList(currentPage, pageSize, param).then(res => {
            const data = res.data.data;
            //分页查询信息
            callback({
              total: data.total,
              data: data.records
            });
          });
        },
        props: {
          label: 'productName',
          value: 'productKey'
        },
        span: 24,
        slot: true,
        rules: [{
          required: true,
          message: '请选择设备所属产品',
          trigger: 'change'
        }]
      },
      {
        label: '设备类型',
        prop: 'deviceType',
        type: 'radio',
        value: 'device',
        dicData: [{
          label: '设备',
          value: 'device'
        }, {
          label: '网关设备',
          value: 'gateway'
        }],
        span: 24,
        gridRow: true,
        search: true,
        rules: [{
          required: true,
          message: '请选择设备类型',
          trigger: 'blur'
        }]
      },
      {
        label: '是否启用',
        prop: 'status',
        type: 'radio',
        dicUrl: '/blade-system/dict/dictionary?code=yes_no',
        dataType: 'number',
        props: {
          label: 'dictValue',
          value: 'dictKey'
        },
        value: 1,
        search: true,
        slot: true,
        display: false,
        width: 90,
        span: 24,
        gridRow: true
      },
      {
        label: '设备状态',
        prop: 'deviceStatus',
        type: 'select',
        dicData: [{
          label: '未激活',
          value: 0
        }, {
          label: '在线',
          value: 1
        }, {
          label: '离线',
          value: 2
        }, {
          label: '禁用',
          value: 4
        }],
        slot: true,
        search: true,
        display: false,
        span: 24,
        gridRow: true
      },
      {
        label: '设备图标',
        prop: 'logo',
        type: 'upload',
        listType: 'picture-img',
        propsHttp: {
          res: 'data',
          url: 'link'
        },
        action: '/blade-resource/oss/endpoint/put-file',
        tip: '只能上传jpg/png文件，且不超过500kb',
        hide: true,
        row: true,
        span: 24
      },
      {
        label: '最后上线时间',
        prop: 'lastOnlineTime',
        type: 'input',
        display: false,
        span: 24,
        gridRow: true
      },
      {
        label: '设备描述',
        prop: 'deviceDesc',
        component: 'avue-ueditor',
        action: '/blade-resource/oss/endpoint/put-file',
        propsHttp: {
          res: 'data',
          url: 'link'
        },
        hide: true,
        minRows: 6,
        span: 24,
        gridRow: true
      },
      {
        label: '',
        prop: 'imgBox',
        display: false
      }
    ]
  };
};

export const optionProperty = {
  height: 'auto',
  addBtn: false,
  editBtn: false,
  delBtn: false,
  viewBtn: false,
  grid: true,
  gridSpan: 8,
  column: [
    {
      label: '属性名称',
      prop: 'name'
    },
    {
      label: '属性标识符',
      prop: 'identifier',
      gridRow: true
    },
    {
      label: '数据类型',
      prop: 'fieldTypeName',
      gridRow: true
    },
    {
      label: '数据定义',
      prop: 'dataTypeName',
      gridRow: true
    },
    {
      label: '属性最新值',
      prop: 'actualValue',
      gridRow: true
    }
  ]
};

export const optionPropertyHistory = {
  height: 'auto',
  calcHeight: 372,
  header: false,
  addBtn: false,
  editBtn: false,
  delBtn: false,
  viewBtn: false,
  menu: false,
  column: [
    {
      label: '更新时间',
      prop: 'time',
      width: 200
    },
    {
      label: '最新值',
      prop: 'actualValue'
    }
  ]
};

export const optionCommand = {
  height: 'auto',
  calcHeight: 322,
  header: false,
  addBtn: false,
  editBtn: false,
  delBtn: false,
  viewBtn: false,
  menu: false,
  column: [
    {
      label: '更新时间',
      prop: 'time'
    },
    {
      label: '标识符',
      prop: 'identifier'
    },
    {
      label: '命令名称',
      prop: 'commandName'
    },
    {
      label: '输入参数',
      prop: 'input'
    },
    {
      label: '输出参数',
      prop: 'output'
    }
  ]
};

export const optionEvent = {
  height: 'auto',
  calcHeight: 372,
  header: false,
  addBtn: false,
  editBtn: false,
  delBtn: false,
  viewBtn: false,
  menu: false,
  column: [
    {
      label: '更新时间',
      prop: 'time'
    },
    {
      label: '标识符',
      prop: 'identifier'
    },
    {
      label: '事件名称',
      prop: 'eventName'
    },
    {
      label: '事件类型',
      prop: 'eventType',
      type: 'select',
      dicData: [{
        label: '信息',
        value: 'info'
      }, {
        label: '告警',
        value: 'alert'
      }, {
        label: '故障',
        value: 'error'
      }]
    },
    {
      label: '输出参数',
      prop: 'output'
    }
  ]
};
